package nothing;

import java.util.Comparator;
import java.util.List;
import nothing.dao.FoodTypeDao;
import nothing.model.Food;
import nothing.model.FoodType;
import nothing.model.Ingredient;
import org.apache.log4j.Logger;

public class IngredientComparator implements Comparator<Ingredient> {
    private FoodTypeDao foodTypeDao = null;
    static Logger log = Logger.getLogger(IngredientComparator.class);

    public IngredientComparator(FoodTypeDao foodTypeDao) {
        setFoodTypeDao(foodTypeDao);
    }
    public void setFoodTypeDao(FoodTypeDao foodTypeDao) {
        this.foodTypeDao = foodTypeDao;
    }

    public int compare(Ingredient ingrediant1, Ingredient ingrediant2) {
        
        Food food1 = ingrediant1.getFood();
        Food food2 = ingrediant2.getFood();
        
        List typeList1 = foodTypeDao.findByFoodName(food1.getName());
        if (typeList1.size() == 0){
            return 1;
        }
        Object[] typeListResult1 = (Object[]) typeList1.get(0);
        if (typeListResult1.length ==0){
            return 1;
        }        
        FoodType foodType1 = (FoodType) typeListResult1[0];
        
        List typeList2 = foodTypeDao.findByFoodName(food2.getName());
        if (typeList2.size() == 0){
            return -1;
        }        
        Object[] typeListResult2 = (Object[]) typeList2.get(0);
        if (typeListResult2.length ==0){
            return -1;
        }        
        FoodType foodType2 = (FoodType) typeListResult2[0];
        
        //log.debug("*****"+foodType2.getFood().getName()+" "+foodType2.getType().getName()+" vrs "+foodType1.getFood().getName()+" "+foodType1.getType().getName());
        return foodType2.getType().getName().compareTo(foodType1.getType().getName());
    }
            
}
